package cn.iocoder.yudao.module.dify.controller.admin.fileprocessing;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import javax.xml.ws.Response;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getLoginUserId;

import cn.iocoder.yudao.module.dify.controller.admin.fileprocessing.vo.*;
import cn.iocoder.yudao.module.dify.dal.dataobject.fileprocessing.FileProcessingDO;
import cn.iocoder.yudao.module.dify.service.fileprocessing.FileProcessingService;
import reactor.core.publisher.Flux;

@Tag(name = "管理后台 - Dify文件处理跟踪")
@RestController
@RequestMapping("/dify/file-processing")
@Validated
@Slf4j
public class FileProcessingController {

    @Resource
    private FileProcessingService fileProcessingService;

    @PostMapping("/create")
    @Operation(summary = "创建Dify文件处理跟踪")
    @PreAuthorize("@ss.hasPermission('dify:file-processing:create')")
    public CommonResult<Long> createFileProcessing(@Valid @RequestBody FileProcessingSaveReqVO createReqVO) throws IOException {
        return success(fileProcessingService.createFileProcessing(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新Dify文件处理跟踪")
    @PreAuthorize("@ss.hasPermission('dify:file-processing:update')")
    public CommonResult<Boolean> updateFileProcessing(@Valid @RequestBody FileProcessingSaveReqVO updateReqVO) {
        fileProcessingService.updateFileProcessing(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除Dify文件处理跟踪")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dify:file-processing:delete')")
    public CommonResult<Boolean> deleteFileProcessing(@RequestParam("id") Long id) {
        fileProcessingService.deleteFileProcessing(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得Dify文件处理跟踪")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dify:file-processing:query')")
    public CommonResult<FileProcessingRespVO> getFileProcessing(@RequestParam("id") Long id) {
        FileProcessingDO fileProcessing = fileProcessingService.getFileProcessing(id);
        return success(BeanUtils.toBean(fileProcessing, FileProcessingRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得Dify文件处理跟踪分页")
    @PreAuthorize("@ss.hasPermission('dify:file-processing:query')")
    public CommonResult<PageResult<FileProcessingRespVO>> getFileProcessingPage(@Valid FileProcessingPageReqVO pageReqVO) {
        log.info("元神启动");
        PageResult<FileProcessingDO> pageResult = fileProcessingService.getFileProcessingPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FileProcessingRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出Dify文件处理跟踪 Excel")
    @PreAuthorize("@ss.hasPermission('dify:file-processing:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFileProcessingExcel(@Valid FileProcessingPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FileProcessingDO> list = fileProcessingService.getFileProcessingPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "Dify文件处理跟踪.xls", "数据", FileProcessingRespVO.class,
                        BeanUtils.toBean(list, FileProcessingRespVO.class));
    }


    @PostMapping(value = "/dify-generate-RistReportFile")
    @Operation(summary = "dify调用生成文件工作流-风险管理报告")
    public CommonResult<Boolean> generatrFile(@RequestParam("id")Long id) throws IOException {
         fileProcessingService.workFolowRistManagerReport(id);
        return success(true);
    }


    @GetMapping(value = "/simple-list")
    @Operation(summary = "获得用户上传文件名称和对应dify的id")
    public CommonResult<List<FileProcessingDO>> getSimpleFileList()  {
        return success(fileProcessingService.getSimpFileleList(getLoginUserId()));
    }

}